package com.ikangtai.shecare.common.baseView.guide;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.PxDxUtil;
import com.ikangtai.shecare.personal.LoginActivity;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends Activity implements View.OnTouchListener, ViewPager.OnPageChangeListener {

    private ViewPager vp;
    private ViewPagerAdapter vpAdapter;
    private List<View> views;
    private LayoutInflater inflater;

    //引导图片资源
    private static final int[] pics = {R.drawable.guide_1,
            R.drawable.guide_2, R.drawable.guide_3,
            R.drawable.guide_4};

    //记录当前选中位置
    private int currentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_guide);
        initView();
    }

    private void initView() {
        App.saveUserPreference("isFirstRun", false);
        inflater = LayoutInflater.from(this);
        views = new ArrayList<View>();
        views.add(inflater.inflate(R.layout.guid_layout1, null));
        views.add(inflater.inflate(R.layout.guid_layout2, null));
        views.add(inflater.inflate(R.layout.guid_layout3, null));

        final View view4 = inflater.inflate(R.layout.guid_layout4, null);
        views.add(view4);
        Button bnStart = (Button) view4.findViewById(R.id.startapp);
        views.get(views.size() - 1).findViewById(R.id.startapp).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });

        vp = (ViewPager) findViewById(R.id.viewpager);
        //初始化Adapter
        vpAdapter = new ViewPagerAdapter(views);
        vp.setAdapter(vpAdapter);
        //绑定回调
//        vp.addOnPageChangeListener(this);
//        views.get(views.size() - 1).setOnTouchListener(this);

    }

    //当滑动状态改变时调用
    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    //当当前页面被滑动时调用
    @Override
    public void onPageScrolled(int position, float offset, int offsetPixels) {
        // TODO Auto-generated method stub
        AppLog.i("onPageScrolled, vp.getCurrentItem() = " + vp.getCurrentItem() + ",onPageScrolled: position=" + position + ",offset=" + offset);
    }

    @Override
    public void onPageSelected(int position) {
        AppLog.i("onPageSelected, vp.getCurrentItem() = " + vp.getCurrentItem() + ",views.size()=" + views.size());
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

//        switch (event.getAction()) {
//            case MotionEvent.ACTION_UP:
//                if (isLastPage() && withinButtonScope(event)) {
//                    Intent intent = new Intent(GuideImgActivity.this, LoginActivity.class);
//                    startActivity(intent);
//                    finish();
//                }
//                break;
//        }
        return true;
    }

    private boolean isLastPage() {
        return (vp.getCurrentItem() == views.size() - 1);
    }

    private boolean withinButtonScope(MotionEvent event) {
        int x = PxDxUtil.px2dip(GuideActivity.this, event.getX());
        int y = PxDxUtil.px2dip(GuideActivity.this, event.getY());

        return (isWidthXScope(x) && isWidthYScope(y));
    }

    private boolean isWidthXScope(int x) {
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = PxDxUtil.px2dip(GuideActivity.this, metric.widthPixels);     // 屏幕宽度（像素）
        int buttonWidth = PxDxUtil.px2dip(GuideActivity.this, 250);
        AppLog.i("Touch x = " + x + ", left = " + (width - buttonWidth) * 0.5 + ",right=" + (width + buttonWidth) * 0.5);
        return ((x >= (width - buttonWidth) * 0.5) && (x <= (width + buttonWidth) * 0.5));
    }

    private boolean isWidthYScope(int y) {
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int height = PxDxUtil.px2dip(GuideActivity.this, metric.heightPixels);   // 屏幕高度（像素）
        int buttonTop = PxDxUtil.px2dip(GuideActivity.this, 450);
        int buttonBottom = PxDxUtil.px2dip(GuideActivity.this, 50);
        AppLog.i("Touch y = " + y + ", top = " + (height - buttonTop) + ",bottom=" + (height - buttonBottom));
        return ((y >= height - buttonTop) && (y <= height - buttonBottom));
    }

    /**
     * 读取本地资源的图片
     *
     * @param context
     * @param resId
     * @return
     */
    public static Bitmap readBitMap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        //获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }
}
